#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Indexes/InterestRateIndex.h>
#pragma unmanaged 
#include <ql\indexes\swapindex.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Indexes {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ISwapIndex
	public ref class CSwapIndex  : 
            public CInterestRateIndex,
            public Cephei::QL::Indexes::ISwapIndex
	{
	protected: 
		boost::shared_ptr<QuantLib::SwapIndex>* _ppSwapIndex;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::SwapIndex>* _phSwapIndex;
#endif
		Object^ _SwapIndexOwner;     // reference to object that manages the storage for this object
	internal:
		CSwapIndex (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex);
		CSwapIndex (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTermStructure);
        CSwapIndex (boost::shared_ptr<QuantLib::SwapIndex>& childNative, Object^ owner);
        CSwapIndex (QuantLib::SwapIndex& childNative, Object^ owner);
        CSwapIndex (CSwapIndex^ copy);
        CSwapIndex (PLATFORM::Type^ t);
#ifdef STRUCT
        CSwapIndex (QuantLib::SwapIndex childNative);
#endif       
#ifdef HANDLE
		CSwapIndex (QuantLib::Handle<QuantLib::SwapIndex>& childNative, Object^ owner);
		CSwapIndex (QuantLib::Handle<QuantLib::SwapIndex> childNative);
#endif
		virtual ~CSwapIndex ();
		!CSwapIndex ();

	internal:
		QuantLib::SwapIndex& GetReference ();
		boost::shared_ptr<QuantLib::SwapIndex>& GetShared ();
		QuantLib::SwapIndex* GetPointer ();
        void SetSwapIndex (boost::shared_ptr<QuantLib::SwapIndex> native)
        {
            if (_ppSwapIndex != NULL)
                delete _ppSwapIndex;
            _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (native);
            SetInterestRateIndex (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::SwapIndex>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Cephei::QL::Indexes::ISwapIndex^ Clone (Cephei::QL::Termstructures::IYieldTermStructure^ forwarding) ;
        property QL::Times::BusinessDayConventionEnum FixedLegConvention 
        {
		    virtual QL::Times::BusinessDayConventionEnum get () ;
        }
        property Cephei::QL::Times::IPeriod^ FixedLegTenor 
        {
		    virtual Cephei::QL::Times::IPeriod^ get () ;
        }
        property Cephei::QL::Indexes::IIborIndex^ IborIndex 
        {
		    virtual Cephei::QL::Indexes::IIborIndex^ get () ;
        }
		virtual DateTime MaturityDate (DateTime valueDate) ;
		virtual Cephei::QL::Instruments::IVanillaSwap^ UnderlyingSwap (DateTime fixingDate) ;
        property Cephei::QL::Termstructures::IYieldTermStructure^ ForwardingTermStructure 
        {
		    virtual Cephei::QL::Termstructures::IYieldTermStructure^ get () ;
        }
        property Cephei::QL::Termstructures::IYieldTermStructure^ DiscountingTermStructure 
        {
		    virtual Cephei::QL::Termstructures::IYieldTermStructure^ get () ;
        }
        property Boolean ExogenousDiscount 
        {
		    virtual Boolean get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ISwapIndex Vector interface
	ref class CSwapIndexVector sealed : 
							    public Core::INativeVector<ISwapIndex^>,
							    public Core::INativeVector<Core::Generic::ICoCell<ISwapIndex^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::SwapIndex>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::SwapIndex>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::SwapIndex>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CSwapIndexVector ();
#ifdef STRUCT
		CSwapIndexVector (std::vector<QuantLib::SwapIndex>& vec, Object^ owner);
		CSwapIndexVector (std::vector<QuantLib::SwapIndex> vec);
#endif       
		CSwapIndexVector (std::vector<boost::shared_ptr<QuantLib::SwapIndex>>& vec, Object^ owner);
#ifdef HANDLE
		CSwapIndexVector (std::vector<QuantLib::Handle<QuantLib::SwapIndex>>& vec, Object^ owner);
#endif
		virtual ~CSwapIndexVector ();
		!CSwapIndexVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property ISwapIndex^ default[int] 
		{
			virtual ISwapIndex^ get (int index) = Core::INativeVector<ISwapIndex^>::default::get;
			virtual void set (int index, ISwapIndex^ value) = Core::INativeVector<ISwapIndex^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<ISwapIndex^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<ISwapIndex^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<ISwapIndex^>^ value) = Core::INativeVector<Core::Generic::ICoCell<ISwapIndex^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::SwapIndex>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::SwapIndex>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::SwapIndex>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>::typeid)]
	[FactoryFor(Cephei::QL::Indexes::ISwapIndex::typeid)]
	[FactoryFor(Cephei::QL::Indexes::ISwapIndex_Factory::typeid)]
	public ref class CSwapIndex_Factory sealed : public ISwapIndex_Factory
	{
	public:
        virtual ISwapIndex^ Create (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex);
        virtual ISwapIndex^ Create (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTermStructure);
        /* <summary> Create a hybrid Vector of ISwapIndex, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<ISwapIndex^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of ISwapIndex, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<ISwapIndex^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<ISwapIndex^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<ISwapIndex^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<ISwapIndex^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<ISwapIndex^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Indexes::ISwapIndex^>^ CreateNativeVector ()
		{
			return REFNEW CSwapIndexVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Indexes::ISwapIndex^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Indexes::ISwapIndex^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Indexes */}
